mod gui;
mod core;
mod utils;

use slint::PlatformError;
use crate::gui::MainWindow;

#[tokio::main]
async fn main() -> Result<(), PlatformError> {
    let window = MainWindow::new()?;
    let weak_window = window.as_weak();
    
    // 初始化核心模块
    let linglong_installer = core::linglong::LinglongInstaller::new();
    let flatpak_installer = core::flatpak::FlatpakInstaller::new();
    let snap_installer = core::snap::SnapInstaller::new();

    // 绑定安装事件
    window.global::<gui::MainEvents>().on_install_requested({
        let weak = weak_window.clone();
        move |pkg_type, path| {
            let weak = weak.clone();
            tokio::spawn(async move {
                let result = match pkg_type.as_str() {
                    "linglong" => linglong_installer.install(&path).await,
                    "flatpak" => flatpak_installer.install(&path).await,
                    "snap" => snap_installer.install(&path).await,
                    _ => Err("未知包类型".into()),
                };
                
                if let Some(window) = weak.upgrade() {
                    window.global::<gui::MainEvents>()
                        .notify_install_result(result.is_ok());
                }
            });
        }
    });

    window.run().unwrap();
    Ok(())
}